<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Item.php");
require_once("../../data/Report.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/text.php");
require_once("HTML/QuickForm/password.php");
require_once("HTML/QuickForm/select.php");
require_once("HTML/QuickForm/submit.php");
require_once("HTML/QuickForm/select.php");

/**
 * Profile form.<br/>
 * Form to be used to search items or reports.
 */
class SearchPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;
        
        $l_searchObjectSelect=new HTML_QuickForm_select("searchedObject");
        $l_searchObjectSelect->setLabel("Type of object searched:");
        $l_searchObjectSelect->addOption("Action","A");
        $l_searchObjectSelect->addOption("Report","R");
        $l_searchObjectSelect->setSelected("A");
        $this->addElement($l_searchObjectSelect);

		$l_searchQueryInput=new HTML_QuickForm_text("searchQuery");
		$l_searchQueryInput->setSize(60);
		$l_searchQueryInput->setLabel("Query:");
		$this->addElement($l_searchQueryInput);
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("Search");
		$this->addElement($l_submit);
		
		//The validation rules (either client and server)
		$this->addRule("searchQuery","The search query must not be empty","required",null,"client");
		$this->addRule("searchQuery","The search query must not be empty","required",null,"server");
		$this->addRule("alphanumeric","Invalid search query (only alphanumeric characters)","required",null,"client");
		$this->addRule("alphanumeric","Invalid search query (only alphanumeric characters)","required",null,"server");
		$this->addRule("searchedObject","Type of object is mandatory","required",null,"server");
		
		$this->applyFilter("searchQuery","trim");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){   	
    	SessionManager::getInstance()->checkSession();
    	displayTop("Search",null,null);
    	?>
<h1>Enter your search query</h1>
		<?php
        $page->display();
        displayBottom();
    }
}

/**
 * Callback function used by array_filter.<br>
 * Remove the Item instances which are not accessible by the currently logged user
 * @param Item $p_item
 * @return boolean
 */
function filterUnallowedActions($p_item){
	return $p_item->isAccessAllowed(SessionManager::getCurrentLoggedUserId());
}

/**
 * Callback function used by array_filter.<br>
 * Remove the Report instances which are not accessible by the currently logged user
 * @param Report $p_report
 * @return boolean
 */
function filterUnallowedReports($p_report){
	return $p_report->isAccessAllowed(SessionManager::getCurrentLoggedUserId());
}

/**
 * Action to be called after a submit action.<br>
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
 		displayTop("Basic search features","../../resources/ajaxscripts.php",null);
    	
    	$l_searchQuery=$page->exportValue("searchQuery");
    	if($page->exportValue("searchedObject")=="A"){
    		//Search for Actions
    		$l_actions=Item::getFromCustomCriteria(
    			"WHERE ItemType_id=:ItemType_id AND (Description LIKE :Description OR Title LIKE :Title) ORDER BY TargetDate",
    			array(
    				"ItemType_id" =>Item::ACTION_TYPE,
    				"Description" =>"%" . $l_searchQuery ."%",
    				"Title" =>"%" . $l_searchQuery ."%"));
    		$l_actions=array_filter($l_actions,"filterUnallowedActions");
    		?>
    		<h1><?=count($l_actions) ?> record(s) found</h1>
    		<?php
    		if(count($l_actions)>0){
				$l_actionsTable=new TableData("ActionsTable");
				$l_actionsTable->addHeader("title","Action");
				$l_actionsTable->addHeader("activityLabel","Activity");
				$l_actionsTable->addHeader("allocated","Allocated");
				$l_actionsTable->addHeader("overdueHtmlFlag","Overdue");
				$l_actionsTable->setZoomArea("../dashboard/ajax_action_details.php");
				$l_actionsTable->setRecords($l_actions);
				displayTable($l_actionsTable);
    		}
    	}else{
    		//Search for reports
    		$l_reports=Report::getFromCustomCriteria("WHERE notes LIKE :notes order by createdDate",
    			array(
    				"notes" => "%" . $l_searchQuery ."%"
    			));
    		$l_reports=array_filter($l_reports,"filterUnallowedReports");
    	?>
    		<h1><?=count($l_reports) ?> record(s) found</h1>
    		<?php
    		if(count($l_reports)>0){
				$l_reportsTable=new TableData("ReportsTable");
				$l_reportsTable->addHeader("createdDate","Report creation date");
				$l_reportsTable->addHeader("userFullName","Submitted by");
				$l_reportsTable->setRecords($l_reports);
				$l_reportsTable->setZoomArea("../report/ajax_report_details.php");
				displayTable($l_reportsTable);
    		}
    	}
    	?>
    	<p>
    	<a href="index.php">New search</a>
    	</p>
    	<?php
    	displayBottom();
    }
}

$controller =new HTML_QuickForm_Controller('BasicSearchProcess', false);
$controller->container(true);
$controller->addPage(new SearchPage('basicSearchProcessPage'));
$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();

?>